package com.fire.social.utils;

import com.fire.social.entity.node.Content;
import com.fire.social.entity.node.Hobby;
import com.fire.social.entity.node.User;
import com.fire.social.mapper.*;
import net.datafaker.Faker;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 生成模拟数据
 */

@Component
public class GenerateUtil {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HobbyMapper hobbyMapper;
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private PraiseMapper praiseMapper;

    public static Map<Long, String> hobbyMap = new HashMap<>();
    static {
        hobbyMap.put(1L, "soccer");
        hobbyMap.put(2L, "basketball");
        hobbyMap.put(3L, "sketch");
        hobbyMap.put(4L, "rock");
        hobbyMap.put(5L, "pop");
        hobbyMap.put(6L, "western food");
        hobbyMap.put(7L, "Landscape");
        hobbyMap.put(8L, "badminton");
        hobbyMap.put(9L, "lol");
        hobbyMap.put(10L, "comedy");
        hobbyMap.put(11L, "self-driving");
        hobbyMap.put(12L, "hiking");
        hobbyMap.put(0L, "novel");
    }

    @Transactional(rollbackFor = Exception.class)
    public void generate() {

        Faker faker = new Faker();
        Random random = new Random();

        List<String> accountList = new ArrayList<>();
        // 随机生成20000个用户
        for (int i = 0; i < 2000; i++) {

            String account = faker.internet().username();
            String password = faker.internet().password();
            String nickName = faker.name().fullName();
            int age = faker.number().numberBetween(18, 40);
            String gender = random.nextBoolean() ? "male" : "female";
            String email = faker.internet().emailAddress();
            String imgurl = faker.avatar().image();
            userMapper.createUser(account, password, nickName, age, gender, email, imgurl);
            accountList.add(account);
            // 随机选几个兴趣爱好
            for (int j = 0; j < random.nextInt(2) + 1; j++) {
                likeMapper.likeHobby(account, Long.parseLong(String.valueOf(random.nextInt(13))));
            }
            // 发布内容
            int contentNum = 3;
            if (i % 100 == 0) {
                contentNum = 5;
            }
            if (i % 300 == 0) {
                contentNum = 10;
            }
            for (int j = 0; j < random.nextInt(contentNum); j++) {
                String title = faker.book().title();
                String content = faker.lorem().paragraph();
                Timestamp past = faker.date().past(365, TimeUnit.DAYS);
                Date time = new Date(past.getTime());
                String timeStr = LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                String image = faker.internet().image();
                Long num = Long.parseLong(String.valueOf(random.nextInt(13))) +1;
                contentMapper.createContent(account, account, imgurl, title, content, hobbyMap.get(num), num, image, timeStr);
            }
        }

        // 关注
        for (int i = 0; i < accountList.size(); i++) {
            int follownum = 5;
            if (i % 50 == 0) {
                follownum = 50;
            }
            if (i % 150 == 0) {
                follownum = 200;
            }
            for (int j = 0; j < random.nextInt(follownum) + 1; j++) {
                int index = random.nextInt(accountList.size());
                if (i == index) continue;
                followMapper.createFollow(accountList.get(i), accountList.get(index));
            }
        }

        // 点赞
        List<Long> contentIdList = contentMapper.findAllContentIds();
        for (int i = 0; i < contentIdList.size(); i++) {
            int praiseNum = 5;
            if (i % 30 == 0) {
                praiseNum = 50;
            }
            if (i % 50 == 0) {
                praiseNum = 200;
            }
            for (int j = 0; j < random.nextInt(praiseNum) + 1; j++) {
                int index = random.nextInt(accountList.size());
                praiseMapper.praisedIt(accountList.get(index), contentIdList.get(i));
            }
        }


    }

    public void generateHobby() {
        hobbyMapper.createHobby("novel", "read");
        hobbyMapper.createHobby("soccer", "sport");
        hobbyMapper.createHobby("basketball", "sport");
        hobbyMapper.createHobby("sketch", "draw");
        hobbyMapper.createHobby("rock", "music");
        hobbyMapper.createHobby("pop", "music");
        hobbyMapper.createHobby("western food", "cook");
        hobbyMapper.createHobby("Landscape", "photography");
        hobbyMapper.createHobby("badminton", "sport");
        hobbyMapper.createHobby("lol", "game");
        hobbyMapper.createHobby("comedy", "movie");
        hobbyMapper.createHobby("self-driving", "travel");
        hobbyMapper.createHobby("hiking", "旅行");
    }

}
